#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
  int incrementMessage=0;
  const int arraySizeIncrease = 1000000;
  int arraySize = arraySizeIncrease, wordCount = 0;
  char** wordListArray = new char* [arraySize];
  char wordSize [512];
  if ( argc != 3 ) {
	cout<<"usage: "<< argv[0] <<" <input_filename> <output_filename>\n";
	abort();
  }
  else {
    ifstream T ( argv[1] );
	ofstream fileOutputStreamTemp ("parseSms");
	char ch;
	while(!T.eof()){
		T.get(ch);
		int  newkey   = ch;
		char ASCII    = newkey & 0xff;
		if((ASCII >= 48 && ASCII <= 57) || (ASCII >= 65 && ASCII <= 90) || (ASCII >= 97 && ASCII <= 122))
			fileOutputStreamTemp << ch;
		else
			fileOutputStreamTemp << "\t";
	}
	fileOutputStreamTemp.close();
    T.close();
	ifstream the_file ( "parseSms"  );
    if ( !the_file.is_open() )
		cout<<"Could not open file\n";
    else {
		while (the_file >> wordSize )
		{
			wordListArray[wordCount] = new char [strlen (wordSize) + 1];
			strcpy (wordListArray[wordCount], wordSize);
			wordCount++;
			//If the File has more then 1000000 Words!
			if (wordCount % arraySizeIncrease == 0)
			{
				char** oldSize = wordListArray;
				arraySize += arraySizeIncrease;
				wordListArray = new char* [arraySize];
				memcpy (wordListArray, oldSize, wordCount * sizeof(char*));
				delete oldSize;
			}
		}
	}
	the_file.close();
  }
		int cntInc=0;
		int cntNum=0;
		int IncElem [wordCount];
		for (int cntIndex = cntInc; cntIndex < wordCount; cntIndex++){	
				float wordFloat = atof(wordListArray[cntIndex]);
				const int bits = *(int*)&wordFloat;
				int exponent = ((bits & 0x7F800000) >> 23) - 127;
				if((exponent==39)||(exponent==33)){
					IncElem[cntNum]=cntInc+1;
					//cout << cntIndex << "CI\t" << cntInc << "CIn\t" << IncElem[cntNum] << "Im\t" << wordListArray[cntIndex] << "\t" << wordFloat << "\t" << exponent << "\n";
					cntInc=0;
					cntNum++;
				}
				else{
					cntInc++;
				}
		}

  	ofstream fileOutputStream (argv[2]);
	for (int cntIndex = 0, cntNumOL=0; cntIndex < wordCount; cntIndex+=(IncElem[cntNumOL]), cntNumOL++)
	{
		if(cntNumOL<cntNum){
		for (int cntIndexIn = cntIndex; cntIndexIn < cntIndex+IncElem[cntNumOL]; cntIndexIn++)
		//for (int cntIndexIn = cntIndex+IncElem[cntNumOL]-1; cntIndexIn >= cntIndex ; cntIndexIn--)
		{
			cout << cntIndex << "CI\t" << cntIndexIn << "CIn\t" <<  cntNumOL << "CNumOL\t" << cntNum << "CNum\t" << IncElem[cntNumOL] << "Im\t" << wordListArray[cntIndexIn] << "\n";
			fileOutputStream << wordListArray[cntIndexIn] << ",";
		}
		fileOutputStream  << "\n";
		}
	}
	fileOutputStream.close();

/*
  	ofstream fileOutputStream (argv[2]);
		int cntInc=0;
		for (int cntIndex = cntInc; cntIndex < wordCount; cntIndex+=incrementMessage){	
			for (int cntIndexIn = cntIndex; cntIndexIn < cntIndex+incrementMessage; cntIndexIn++){
				float wordFloat = atof(wordListArray[cntIndexIn]);
				const int bits = *(int*)&wordFloat;
				int exponent = ((bits & 0x7F800000) >> 23) - 127;
				cout << cntIndex << "\t" << cntIndexIn << "\t" << incrementMessage << "\t" << wordListArray[cntIndexIn] << "\t" << wordFloat << "\t" << exponent << "\n";
				if(exponent==39||exponent==33){
					incrementMessage=cntInc+1;;
					fileOutputStream << wordListArray[cntIndexIn] << ",";
					//break;
				}
				else{
					cntInc++;
					fileOutputStream << wordListArray[cntIndexIn] << ",";
				}
			}
		fileOutputStream  << "\n";
	}
	fileOutputStream.close();
*/

	for (int cntIndexN = 0; cntIndexN < wordCount; cntIndexN++)
	{
		delete wordListArray[cntIndexN];
	}
	delete wordListArray;
	wordListArray = NULL;
	cout << "\nOutput file Written.\n\n";
}

